package com.gjintel.business.webcontent.service.Impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.webcontent.dao.IWebContentDao;
import com.gjintel.business.webcontent.dto.Combo;
import com.gjintel.business.webcontent.model.WebContent;
import com.gjintel.business.webcontent.service.IWebContentService;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.dataset.model.DataSet;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.base.service.impl.BaseServiceImpl;
import com.gjintel.sdk.util.StringUtil;

@Service("webContentService")
public class WebContentServiceImpl extends BaseServiceImpl implements IWebContentService {
	@Autowired
	private IWebContentDao webContentDao;
	
	/**未开标*/
	private int NOOPENBID=0;
	/**已开标*/
	private int OPENBID=1;
	/**正在公式中*/
	private int PUBLICITY_IN=2;
	/**公示结束*/
	private int PUBLICITY_END=3;
	
	@Override
	public List<WebContent> findAll() {
		return this.webContentDao.findAll();
	}
	
	@Override
	public List<WebContent> findAll(String id) {
		return this.webContentDao.findAll(id);
	}
	
	@Override
	public List<WebContent> findAllByType(String type,Page page,String deptId) {
		return this.webContentDao.findAllByType(type,page,deptId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void findAll(Page page, WebContent webContent, Sort sort,java.sql.Date startTime,java.sql.Date endTime,String deptId,Boolean noEditPubType) {
		this.webContentDao.findAll(page, webContent, sort, startTime, endTime,deptId,noEditPubType);
//		List<WebContent> result = page.getResult();
//		for (WebContent web : result) {
//			ProjectInfo project = this.get(ProjectInfo.class, web.getProjectId());
//			if(project!=null&&project.getBidStartTime()!=null&&web.getType().equals("31")){//竞价公示
//				if (project.getBidStartTime().compareTo(new Date())<=0) {//到了开标时间
//					web.setIsOpen(OPENBID);
//				}else{
//					web.setIsOpen(NOOPENBID);
//				}
//			}
//			if(project!=null&&project.getHitShowStartTime()!=null&&(web.getType().equals("32"))){//中标公示
//				if (project.getShowEndTime().compareTo(new Date())<=0) {//到了中标公示结束时间
//					web.setIsOpen(PUBLICITY_END);
//				}else{
//					web.setIsOpen(PUBLICITY_IN);
//				}
//			}
//			if(project!=null&&project.getFlowShowEndTime()!=null&&(web.getType().equals("33"))){//流标公示
//				if (project.getFlowShowEndTime().compareTo(new Date())<=0) {//到了流标公示结束时间
//					web.setIsOpen(PUBLICITY_END);
//				}else{
//					web.setIsOpen(PUBLICITY_IN);
//				}
//			}
//			if(project!=null&&project.getContactEndTime()!=null&&(web.getType().equals("41"))){//合同公示
//				if (project.getContactEndTime().compareTo(new Date())<=0) {//到了合同公示结束时间
//					web.setIsOpen(PUBLICITY_END);
//				}else{
//					web.setIsOpen(PUBLICITY_IN);
//				}
//			}
//			if(project!=null&&project.getReceivingEndTime()!=null&&(web.getType().equals("42"))){//验收报告公示
//				if (project.getReceivingEndTime().compareTo(new Date())<=0) {//到了验收报告结束时间
//					web.setIsOpen(PUBLICITY_END);
//				}else{
//					web.setIsOpen(PUBLICITY_IN);
//				}
//			}
//			if(web.getType().equals("34")){//变更公告公示
//				Date changeEndTime=null;
//				try {
//					//获取变更公告公示期
//					DataSet dataset = this.get(DataSet.class, Constant.DATASET_ID);
//					changeEndTime=new SimpleDateFormat("yyyy-MM-dd HH:smm:ss")//
//						.parse(StringUtil.computeDate(web.getCreateTime(), "00:00:00", dataset.getChangePublicity()+1));
//				} catch (ParseException e) {
//					e.printStackTrace();
//				}
//				if (changeEndTime.compareTo(new Date())<=0) {//变更公告公示
//					web.setIsOpen(PUBLICITY_END);
//				}else{
//					web.setIsOpen(PUBLICITY_IN);
//				}
//			}
//		}
	}

	@Override
	public void removeData(String deleteIds) throws Exception {
		WebContent webmanage = null;
		for (String id : deleteIds.split(",")) {
			webmanage = this.get(WebContent.class, id);
			if (webmanage != null) {
				webmanage.setFlag(1);
			}
		}
	}

	@Override
	public List<WebContent> findListByProjectId(String project) {
		return this.webContentDao.findListByProjectId(project);
	}

	@Override
	@Deprecated
	public List<WebContent> findListByType(WebContent WebContent) {
		 List<WebContent> list =this.webContentDao.findListByType(WebContent);
			for (WebContent web : list) {
				ProjectInfo project = this.get(ProjectInfo.class, web.getProjectId());
				if(project!=null&&project.getBidStartTime()!=null&&web.getType().equals("31")){//竞价公示
					if (project.getBidStartTime().compareTo(new Date())<=0) {//到了开标时间
						web.setIsOpen(OPENBID);
					}else{
						web.setIsOpen(NOOPENBID);
					}
				}
				if(project!=null&&project.getHitShowStartTime()!=null&&(web.getType().equals("32"))){//中标公示
					if (project.getShowEndTime().compareTo(new Date())<=0) {//到了中标公示结束时间
						web.setIsOpen(PUBLICITY_END);
					}else{
						web.setIsOpen(PUBLICITY_IN);
					}
				}
				if(project!=null&&project.getFlowShowEndTime()!=null&&(web.getType().equals("33"))){//流标公示
					if (project.getFlowShowEndTime().compareTo(new Date())<=0) {//到了流标公示结束时间
						web.setIsOpen(PUBLICITY_END);
					}else{
						web.setIsOpen(PUBLICITY_IN);
					}
				}
				if(project!=null&&project.getContactEndTime()!=null&&(web.getType().equals("41"))){//合同公示
					if (project.getContactEndTime().compareTo(new Date())<=0) {//到了合同公示结束时间
						web.setIsOpen(PUBLICITY_END);
					}else{
						web.setIsOpen(PUBLICITY_IN);
					}
				}
				if(project!=null&&project.getReceivingEndTime()!=null&&(web.getType().equals("42"))){//验收报告公示
					if (project.getReceivingEndTime().compareTo(new Date())<=0) {//到了验收报告结束时间
						web.setIsOpen(PUBLICITY_END);
					}else{
						web.setIsOpen(PUBLICITY_IN);
					}
				}
				if(web.getType().equals("34")){//变更公告公示
					Date changeEndTime=null;
					try {
						//获取变更公告公示期
						DataSet dataset = this.get(DataSet.class, Constant.DATASET_ID);
						changeEndTime=new SimpleDateFormat("yyyy-MM-dd HH:smm:ss")//
							.parse(StringUtil.computeDate(web.getCreateTime(), "00:00:00", dataset.getChangePublicity()+1));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					if (changeEndTime.compareTo(new Date())<=0) {//变更公告公示
						web.setIsOpen(PUBLICITY_END);
					}else{
						web.setIsOpen(PUBLICITY_IN);
					}
				}
				
			}
		 return list;				 
	}

	@Override
	public List<WebContent> getUnAutoNotice(String type) {
		 List<WebContent> list =this.webContentDao.getUnAutoNotice(type);
//			for (WebContent web : list) {
//				ProjectInfo project = this.get(ProjectInfo.class, web.getProjectId());			
//				if (project!=null&&project.getBidStartTime().compareTo(new Date())<=0) {//到了开标时间
//					web.setIsOpen(1);
//				}
//				
//			}
		return list;
	}

	@Override
	public List<WebContent> findOppugnWebContentList(Page page, WebContent webcontent) {
		return this.webContentDao.findOppugnWebContentList(page,webcontent);
	}

	@Override
	public void findAllJoinProject(Page page, WebContent webcontent) {
		this.webContentDao.findAllJoinProject(page,webcontent);
		List<WebContent> result = page.getResult();
		for (WebContent web : result) {
			
			if(web.getType().equals("32")//中标公示
					||web.getType().equals("33")//流标公示
					||web.getType().equals("34")//变更公告
					||web.getType().equals("41")//合同公示
					||web.getType().equals("42")){//验收报告公示
				if (web.getEndShowTime()!=null&&web.getEndShowTime().compareTo(new Date())<=0) {//展示结束时间
					web.setIsOpen(PUBLICITY_END);
				}else{
					web.setIsOpen(PUBLICITY_IN);
				}
			}else if(web.getType().equals("31")){//竞价公示
				if (web.getEndShowTime()!=null&&web.getEndShowTime().compareTo(new Date())<=0) {//展示结束时间
					web.setIsOpen(OPENBID);//已开标
				}else{
					web.setIsOpen(NOOPENBID);//为开标
				}
			}
		}
	}

	@Override
	public List<WebContent> findWebByBusinessId(String businessId) {
		return this.webContentDao.findWebByBusinessId(businessId);
	}

	@Override
	public List<RegisterUser> findAllComboList() {
		return this.webContentDao.findAllComboList();
	}  

//	@Override
//	public List<String> getWebManageTree(List<TreeDto> treeList, String id,	String nodeName, String selectIds,boolean isDialog) {
//		List<String> selIds = new ArrayList<String>();
//		if (StringUtil.isEmpty(id)) {
//			id = InitCacheData.ROOT;
//			List<TreeDto> rootTreeList = this.getTreePoints(InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.ARTICLE_TYPE), id);
//			TreeDto rootTree = null;
//			if (rootTreeList != null && rootTreeList.size() == 1) {
//				rootTree = rootTreeList.get(0);
//				rootTree.setState("open");
//				List<TreeDto> childTreeList = this.getTreePoints(InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.ARTICLE_TYPE), rootTree.getId());
//				rootTree.setChildren(childTreeList);
//				rootTreeList.clear();
//				rootTreeList.add(rootTree);
//			}
//			treeList.addAll(rootTreeList);
//		} else {
//			treeList.addAll(this.getTreePoints(InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.ARTICLE_TYPE), id));
//		}
//		return selIds;
//	}
	
	/**
	 * @description 根据给点的节点ID，得到该节点下所有的孩子节点对象
	 * @author Ant-K
	 * @date 2016-03-16 19:50
	 * @param childrenDictionaryList
	 * @param id
	 * @return
	 * @return List<TreeDto>
	 */
//	@SuppressWarnings("unchecked")
//	private List<TreeDto> getTreePoints(Map<String, List> childrenArticleList, String id) {
//		List<ArticManage> childrenArticleType = childrenArticleList.get(id);
//		List<TreeDto> treeList = new ArrayList<TreeDto>();
//		if (childrenArticleType != null && !childrenArticleType.isEmpty()) {
//			TreeDto treeDto = null;
//			ArticManage articManage = null;
//			Iterator<ArticManage> dictIt = childrenArticleType.iterator();
//			while (dictIt.hasNext()) {
//				articManage = dictIt.next();
//				treeDto = new TreeDto();
//				treeDto.setIconCls("icon-dict-icon");
//				treeDto.setId(articManage.getId());
//				treeDto.setText(articManage.getName());
//				if (InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.ARTICLE_TYPE).get(treeDto.getId()) != null) {
//					treeDto.setChildren(new ArrayList<TreeDto>());
//					treeDto.setState("closed");
//				}
//				treeList.add(treeDto);
//			}
//		}
//		return treeList;
//	}
}
